'use strict'

//-------------------------6.6 方法-------------------------------------------
Array.method('reduce', function reduce(f, value) {
    var i;
    for (i = 0; i < this.length; i++) {
        value = f(this[i], value);
    }
    return value;
});

//-------------------------6.7 维度-------------------------------------------
/**
 * 初始化数组
 * @param dimension
 * @param initial
 * @returns {Array}
 */
Array.dim = function (dimension, initial) {
    var a = [], i;
    for (i = 0; i < dimension; i++) {
        a[i] = initial;
    }
    return a;
}

var myArray = Array.dim(10, 0);

/**
 * 初始化二维数组
 * @param m
 * @param n
 * @param initial
 * @returns {Array}
 */
Array.matrix = function (m, n, initial) {
    var a, i, j, mat = [];
    for (i = 0; i < m; i += 1) {
        a = [];
        for (j = 0; j < n; j += 1) {
            a[j] = 0;
        }
        mat[i] = a;
    }
    return mat;
}

var myMatrix = Array.matrix(4, 4, 0);

Array.identity = function (n) {
    var i, mat = Array.matrix(n, n, 0);
    for (i = 0; i < n; i += 1) {
        mat[i][i] = 1;
    }
    return mat;
}

myMatrix = Array.identity(4);